import { JSONParse,setComponentPosition } from "@/utils/components"
import { ConfigType,CreateComponentType } from '@/views/drag/package/index.d'
import { createComponent } from '@/views/drag/package/index'
import { useChartEditStore } from '@/store/modules/chartEditStore/index'
let chartEditStore =useChartEditStore()
export const dragHandle = async (e: DragEvent) => {
  e.preventDefault()
  try {
    const drayDataString = e!.dataTransfer!.getData('ChartData')
    if (!drayDataString) {
    return
    }
    const dropData: Exclude<ConfigType, ['image']> = JSONParse(drayDataString)
        // 创建新图表组件
        console.log(2222)
    let newComponent: CreateComponentType = await createComponent(dropData)
    console.log(newComponent)
        if (dropData.redirectComponent) {
          dropData.dataset && (newComponent.option.dataset = dropData.dataset)
          newComponent.chartConfig.title = dropData.title
          newComponent.chartConfig.chartFrame = dropData.chartFrame
    }
    
    setComponentPosition(newComponent, e.offsetX - newComponent.attr.w / 2, e.offsetY - newComponent.attr.h / 2)
    chartEditStore.addComponentList(newComponent, false, true)
    // chartEditStore.setTargetSelectChart(newComponent.id)
  } catch (error) {
  
  }

}
